require 'MVVM.Sources.View.Adaptor'
--require 'View.DOTween.UIAnimationLib'

ButtonAdaptor = class('ButtonAdaptor', Adaptor)
ButtonAdaptor.CLICK_INTERVAL = 20

function ButtonAdaptor:ctor(gameObject, propertyName, binder,disableAnimation, soundEffect,checkFunc)
    self.onClickCallback = nil
    self.lastClickFrameNo = -1

    --self.soundEffect = soundEffect or 'SoundEffect/1_btn'
    self.component = gameObject:GetComponent('Button')
    --if not disableAnimation then
    --    UIAnimationLib.BtnClickAnim(gameObject)
    --end
    binder:RegisterEvent(function(viewModel, property)
        self.callFunc = function()
            if checkFunc  then
                if not checkFunc() then
                    return
                end
            end
            if self.lastClickFrameNo > 0 and FrameNum - self.lastClickFrameNo < ButtonAdaptor.CLICK_INTERVAL then return end
            self.lastClickFrameNo = FrameNum

            local onClick = property['OnClick']
            if onClick == nil then
                return
            end

            --if #self.soundEffect > 0 then
            --    AudioManager.PlaySound(self.soundEffect)
            --end
            onClick(self.component.name,gameObject)
            if self.onClickCallback then
                self.onClickCallback()
            end
        end
        self.component.onClick:AddListener(self.callFunc)
    end, function()
        if self.callFunc~=nil then
            self.component.onClick:RemoveListener(self.callFunc)
        end
        self.onClickCallback = nil
    end, propertyName)
end